package com.kuang.demo02;

/**
 * @author FHL
 */
public class UnsafeBank {
    public static void main(String[] args) {
        Account account=new Account(50,"girl");
       Drawing girl=new Drawing(account,40,"girl");
       Drawing boy=new Drawing(account,100,"boy");
       girl.start();
       boy.start();
    }
}
//账户
class Account{
    //余额
    int money;
    //卡名
    String name;
    public Account(int money,String name){
        this.money=money;
        this.name=name;
    }
}
//模拟取款
class Drawing extends Thread{
    //账户
    Account account;
    //取出的钱
    int drawingMoney;
    //手里剩下的钱
    int nowMoney;
    public Drawing(Account account,int drawingMoney,String name){
        super(name);
        this.account=account;
        this.drawingMoney=drawingMoney;

    }
    //取钱
    @Override
    public void run() {
        //锁的对象就是变化的量，需要增删改的对象
        synchronized (account){
            //判断是否还有钱能被取出来
            if (account.money-drawingMoney<0){
                System.out.println(Thread.currentThread().getName()+"没钱啦！");
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            account.money=account.money-drawingMoney;
            nowMoney=nowMoney+drawingMoney;
            System.out.println(account.name+"的余额为："+account.money);
            System.out.println(this.getName()+"手里的钱为："+nowMoney);
        }
        }

}
